import java.util.ArrayList;
import java.util.Stack;

public class History {

	public Stack<HistoryStatus> undoStack;
	private Stack<HistoryStatus> redoStack;
	private HistoryStatus undoStatus;
	private HistoryStatus undoStatusClone;
	private HistoryStatus redoStatus;
	private HistoryStatus redoStatusClone;
	
	private static final String NO_HISTORY = "No operation has been performed.\n";
	private static final String HISTORY_HEADER = "----- History -----     (the latest operation on top)\n";
	private static final String HISTORY_END = "----- End of history -----\n";


	
	public History() {
		
		undoStack = new Stack<HistoryStatus>();
		redoStack = new Stack<HistoryStatus>();
		
	}
	
	public void storeUndo (ArrayList<Task> list, String command) {
		
		undoStack.push(cloneHistoryStatus(list, command));
		redoStack = new Stack<HistoryStatus>();
		
		
	}
	
	private HistoryStatus cloneHistoryStatus (ArrayList<Task> list, String command) {
		
		ArrayList<Task> tasksClone = new ArrayList<Task>();
		Task singleTaskClone; 
		
		for(int i=0;i<list.size();i++){
			
			singleTaskClone = new Task();
			
			singleTaskClone.setId(list.get(i).getId());
			singleTaskClone.setContent(list.get(i).getContent());
			singleTaskClone.setDeadline(list.get(i).getDeadline());
			singleTaskClone.setStart(list.get(i).getStart());
			singleTaskClone.setEnd(list.get(i).getEnd());
			singleTaskClone.setPriority(list.get(i).isPriority());
			singleTaskClone.setLabel(list.get(i).getLabel());
			singleTaskClone.setDone(list.get(i).isDone());
			singleTaskClone.setUserInput(list.get(i).getUserInput());
			
			tasksClone.add(singleTaskClone);
		}
		
		return new HistoryStatus(tasksClone, command);
		
	}
	
	public HistoryStatus undo ( ArrayList<Task> list ) {
		
/*	
		System.out.println(undoStack.size());
		
		System.out.println("in History -undo " + undoStack.get(1).getTasks().get(0).getContent());
		System.out.println("in History -undo " + undoStack.get(1).getTasks().get(0).isDone());
		
		System.out.println("in History -undo " + undoStack.get(0).getTasks().get(0).getContent());
		System.out.println("in History -undo " + undoStack.get(0).getTasks().get(0).isDone());
*/	
		if(undoStack.size()!=0) {
			
			undoStatus = undoStack.pop();
			undoStatusClone = cloneHistoryStatus(list, undoStatus.getCommand());
			
			redoStack.push(undoStatusClone);
			
		} else {
			
			return null;
			
		}
		
//		System.out.println("in History -undo " + undoStatus.getTasks().get(0).isDone());
		
		return undoStatus;
		
	}
	
	public HistoryStatus redo ( ArrayList<Task> list ) {
		
		if(redoStack.size()!=0) {
			
			redoStatus = redoStack.pop();
			redoStatusClone = cloneHistoryStatus(list, redoStatus.getCommand());
			undoStack.push(redoStatusClone);
			
		} else {
			
			return null;
			
		}
		
		return redoStatus;
		
	}
	
	public String showHistory() {
		
		String historyList = "";
		
		if(undoStack.size()==0) {
			historyList = NO_HISTORY;
		} else {
			
			historyList = historyList + HISTORY_HEADER;
			
			for(int i = undoStack.size()-1 ; i >=0 ; i--) {
				
				historyList = historyList + (undoStack.size() - i) + ". " + undoStack.get(i).getCommand() + "\n";

				
			}
			
			historyList = historyList + HISTORY_END;
			
		}
		
		return historyList;
		
	}
	
	
	
	
	
}
